package com.example.menhoo.toollibrary.common.utils;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.SystemClock;
import android.provider.Settings;
import android.telephony.TelephonyManager;

import com.example.menhoo.toollibrary.android.log.Log;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 手机信息 & MAC地址 & 开机时间
 *
 * @author MaTianyu
 * @date 2014-09-25
 */
public class AndroidUtil {
    private static final String TAG = AndroidUtil.class.getSimpleName();

    /**
     * dp 和 px单位转换
     * @param context
     * @param dpValue
     * @return int px value
     */
    public static int dp2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
     */
    public static int px2dp(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    /**
     * 获取 MAC 地址
     * <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
     */
    public static String getMacAddress(Context context) {
        //wifi mac地址
        WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        WifiInfo info = wifi.getConnectionInfo();
        String mac = info.getMacAddress();
        if (Log.isPrint) {
            Log.i(TAG, " MAC：" + mac);
        }
        return mac;
    }

    /**
     * 获取 开机时间
     */
    public static String getBootTimeString() {
        long ut = SystemClock.elapsedRealtime() / 1000;
        int h = (int) ((ut / 3600));
        int m = (int) ((ut / 60) % 60);
        if (Log.isPrint) {
            Log.i(TAG, h + ":" + m);
        }
        return h + ":" + m;
    }

    /**
     * 获得版本号
     * @param context
     * @return
     */
    public static int getVerCode(Context context) {
        try {
            return context.getPackageManager().getPackageInfo(
                    context.getPackageName(), 0).versionCode;
        } catch (Exception e) {
            Log.e(TAG, "Cannot find package and its version info.");
            return -1;
        }
    }
    /**
     * 获得版本名称
     * @param context
     * @return
     */
    public static String getVerName(Context context) {
        try {
            return context.getPackageManager().getPackageInfo(
                    context.getPackageName() , 0).versionName;
        } catch (Exception e) {
            Log.e(TAG, "Cannot find package and its version info.");
            return "no version name";
        }
    }

    /**
     * 获取DeviceID
     *
     * @param context
     * @return 没有返回null
     */
    public static String getDeviceID(Context context) {
        try {
            TelephonyManager tm = (TelephonyManager) context
                    .getSystemService(Context.TELEPHONY_SERVICE);
            if (tm == null) {
                return null;
            } else {
                String id = tm.getDeviceId();
                return id == null? null : id;
            }
        } catch (Exception e) {
            return null;
        }

    }
    /**
     * 获得ANDROID_ID
     * @param context
     * @return 没有返回null
     */
    public static String getAndroidID(Context context) {
        String androidID = Settings.Secure.getString(context.getContentResolver(),Settings.Secure.ANDROID_ID);
        return androidID;
    }
    /**
     * 获得Serial_ID
     * @param context
     * @return 没有返回null
     */
    public static String getSerialID(Context context) {
        String serial = android.os.Build.SERIAL.equalsIgnoreCase("unknown")?"null":android.os.Build.SERIAL ;
        return serial;
    }
    /**
     * 获得手机唯一标识码,根据Android版本不同获得的信息也不同,首先获得DeviceID,如果没有获得Serial_ID,还没有获得AndroidID
     * @param context
     * @return 没有返回null
     */
    public static String getUniqueID(Context context){
        String unique_identifier=getDeviceID(context);
        if(unique_identifier==null || unique_identifier.equalsIgnoreCase("null")){
            unique_identifier=getSerialID(context);
        }
        if(unique_identifier==null || unique_identifier.equalsIgnoreCase("null")){
            unique_identifier=getAndroidID(context);
        }
        return unique_identifier;
    }

    public static String printSystemInfo() {
        Date date = new Date(System.currentTimeMillis());
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = dateFormat.format(date);
        StringBuilder sb = new StringBuilder();
        sb.append("_______  系统信息  ").append(time).append(" ______________");
        sb.append("\nID                 :").append(Build.ID);
        sb.append("\nBRAND              :").append(Build.BRAND);
        sb.append("\nMODEL              :").append(Build.MODEL);
        sb.append("\nRELEASE            :").append(Build.VERSION.RELEASE);
        sb.append("\nSDK                :").append(Build.VERSION.SDK);

        sb.append("\n_______ OTHER _______");
        sb.append("\nBOARD              :").append(Build.BOARD);
        sb.append("\nPRODUCT            :").append(Build.PRODUCT);
        sb.append("\nDEVICE             :").append(Build.DEVICE);
        sb.append("\nFINGERPRINT        :").append(Build.FINGERPRINT);
        sb.append("\nHOST               :").append(Build.HOST);
        sb.append("\nTAGS               :").append(Build.TAGS);
        sb.append("\nTYPE               :").append(Build.TYPE);
        sb.append("\nTIME               :").append(Build.TIME);
        sb.append("\nINCREMENTAL        :").append(Build.VERSION.INCREMENTAL);

        sb.append("\n_______ CUPCAKE-3 _______");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.CUPCAKE) {
            sb.append("\nDISPLAY            :").append(Build.DISPLAY);
        }

        sb.append("\n_______ DONUT-4 _______");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.DONUT) {
            sb.append("\nSDK_INT            :").append(Build.VERSION.SDK_INT);
            sb.append("\nMANUFACTURER       :").append(Build.MANUFACTURER);
            sb.append("\nBOOTLOADER         :").append(Build.BOOTLOADER);
            sb.append("\nCPU_ABI            :").append(Build.CPU_ABI);
            sb.append("\nCPU_ABI2           :").append(Build.CPU_ABI2);
            sb.append("\nHARDWARE           :").append(Build.HARDWARE);
            sb.append("\nUNKNOWN            :").append(Build.UNKNOWN);
            sb.append("\nCODENAME           :").append(Build.VERSION.CODENAME);
        }

        sb.append("\n_______ GINGERBREAD-9 _______");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
            sb.append("\nSERIAL             :").append(Build.SERIAL);
        }
        Log.i(TAG, sb.toString());
        return sb.toString();
    }

    /**
     * 安装APK
     * @param apkFile
     * @param context
     */
    public static void installApkWithPrompt(File apkFile, Context context) {
        Intent promptInstall = new Intent(Intent.ACTION_VIEW);
        promptInstall.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        promptInstall.setDataAndType(Uri.fromFile(apkFile),
                "application/vnd.android.package-archive");
        context.startActivity(promptInstall);
    }
}
